package zju.xmsx.acm.search.back;

import org.elasticsearch.search.SearchHits;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import zju.xmsx.acm.search.back.dao.ESUpdater;
import zju.xmsx.acm.search.back.dao.ElasticSearcher;
import zju.xmsx.acm.search.back.dto.*;

import javax.annotation.Resource;

@RestController
public class HttpController {

    @Resource
    ElasticSearcher searcher;

    @Resource
    ESUpdater updater;

    @ResponseBody
    @PostMapping("/searchArticleByKW")
    MessageResponse searchArticleByKW(@RequestBody ReqBody param) {

        if (param.getCurPage() == 0 && param.getPageSize() == 0) {
            return new MessageResponse(false, "前端请求缺少必要参数");
        }

        SearchHits res;
        if(param.getFilter() == null){
            res = searcher.searchByTitleKeyWord(param.getKeys(), param.getCurPage() * param.getPageSize(),
                    param.getPageSize());
        } else{
            res = searcher.searchByTitleKeyWord(param.getKeys(), param.getCurPage() * param.getPageSize(),
                    param.getPageSize(), param.getFilter());
        }
        if (res != null) {
            return new MessageResponse(true, "搜索成功", new SearchResults(res));
        } else {
            return new MessageResponse(false, "搜索失败");
        }
    }

    @PostMapping("/mixSearch")
    MessageResponse mixSearch(@RequestBody ReqBody param) {

        if (param.getCurPage() == 0 && param.getPageSize() == 0) {
            return new MessageResponse(false, "前端请求缺少必要参数");
        }

        SearchHits res1;
        if (param.getFilter() != null) {
            res1 = searcher.searchByTitleKeyWord(param.getKeys(), param.getCurPage() * param.getPageSize(),
                    param.getPageSize(), param.getFilter());
        } else {
            res1 = searcher.searchByTitleKeyWord(param.getKeys(), param.getCurPage() * param.getPageSize(),
                    param.getPageSize());
        }

        SearchHits res2 = searcher.searchAuthorList(param.getKeys(), param.getCurPage() * param.getPageSize(),
                param.getPageSize());
        SearchHits res3 = searcher.searchConferenceByKW(param.getKeys(), param.getCurPage() * param.getPageSize(),
                param.getPageSize());
        if (res1 == null || res2 == null || res3 == null)
            return new MessageResponse(false, "搜索失败");

        Object[] res = { new SearchResults(res1), new SearchResults(res2), new SearchResults(res3) };
        return new MessageResponse(true, "搜索成功", res);
    }

    @PostMapping("/searchByAuthorKW")
    MessageResponse searchByAuthorKW(@RequestBody ReqBody param) {
        if (param.getCurPage() == 0 && param.getPageSize() == 0) {
            return new MessageResponse(false, "前端请求缺少必要参数");
        }

        SearchHits res = searcher.searchAuthorList(param.getKeys(), param.getCurPage() * param.getPageSize(),
                param.getPageSize());
        if (res == null) {
            return new MessageResponse(false, "搜索失败");
        }
        return new MessageResponse(true, "搜索成功", new SearchResults(res));
    }

    @PostMapping("/getArticleByAuthorId")
    MessageResponse getArticleByAuthorId(@RequestBody ReqBody param) {
        if (param.getCurPage() == 0 && param.getPageSize() == 0) {
            return new MessageResponse(false, "前端请求缺少必要参数");
        }

        SearchHits res;
        if(param.getFilter() == null){
            res = searcher.searchByAuthorExact(param.getKeys(), param.getCurPage() * param.getPageSize(),
                    param.getPageSize());
        } else{
            res = searcher.searchByAuthorExact(param.getKeys(), param.getCurPage() * param.getPageSize(),
                    param.getPageSize(), param.getFilter());
        }
        if (res != null) {
            return new MessageResponse(true, "搜索成功", new SearchResults(res));
        } else {
            return new MessageResponse(false, "搜索失败");
        }
    }

    @PostMapping("/getConferenceByKW")
    MessageResponse getConferenceByKW(@RequestBody ReqBody param) {
        if (param.getCurPage() == 0 && param.getPageSize() == 0) {
            return new MessageResponse(false, "前端请求缺少必要参数");
        }

        SearchHits res;
        if(param.getFilter() == null){
            res = searcher.searchConferenceByKW(param.getKeys(), param.getCurPage() * param.getPageSize(),
                    param.getPageSize());
        } else{
            res = searcher.searchConferenceByKW(param.getKeys(), param.getCurPage() * param.getPageSize(),
                    param.getPageSize(), param.getFilter());
        }
        if (res != null) {
            return new MessageResponse(true, "搜索成功", new SearchResults(res));
        } else {
            return new MessageResponse(false, "搜索失败");
        }
    }

    @PostMapping("/getArticleByConferenceId")
    MessageResponse getArticleByConferenceId(@RequestBody ReqBody param) {
        if (param.getCurPage() == 0 && param.getPageSize() == 0) {
            return new MessageResponse(false, "前端请求缺少必要参数");
        }

        SearchHits res;
        if(param.getFilter() == null){
            res = searcher.searchByConfExact(param.getKeys(), param.getCurPage() * param.getPageSize(),
                    param.getPageSize());
        } else {
            res = searcher.searchByConfExact(param.getKeys(), param.getCurPage() * param.getPageSize(),
                    param.getPageSize(), param.getFilter());
        }
        if (res != null) {
            return new MessageResponse(true, "搜索成功", new SearchResults(res));
        } else {
            return new MessageResponse(false, "搜索失败");
        }
    }

    @PostMapping("/updateBulk")
    MessageResponse updateBulk(@RequestBody UpdateContent[] param) {
        if(updater.bulkUpdate(param) == null){
            return new MessageResponse(false,"更新失败",null);
        }
        return new MessageResponse(true,"更新成功",null);
    }

    @PostMapping("/updateSingle")
    MessageResponse updateSingle(@RequestBody UpdateContent param) {
        if(updater.updateDoc(param) == null){
            return new MessageResponse(false,"更新失败",null);
        }
        return new MessageResponse(true,"更新成功",null);
    }

}
